import { defineStore } from "pinia";
import { api, API_TSL_FRONTEND } from "boot/axios";
import { Loading } from "quasar";

// For QC WO Review Page
export const useReportStore = defineStore("report", {
  state: () => ({
    // Worker Report Page - deliveryReport (handset)
    dr_goldType: [],

    // Worker Report Page - deliveryEnquiry (transaction)
    de_goldType: [],
    de_workCut: [],

    // QC & Dispatcher Report page - realtimeStockReport
    realtime_handset: [],
    realtime_transaction: [],
  }),

  getters: {},
  actions: {
    // For Worker: DeliverEnquiry(员工交收查询) & DeliverReport(员工交收报表二 (手工帐)) Page
    async getWorkerReportParams(reportType, staffCode, userCode) {
      console.log("params: ", reportType, staffCode, userCode);
      Loading.show();
      console.log("after load function");
      try {
        const res = await api.post(
          API_TSL_FRONTEND + "/report/WorkerReportView/" + "get_report_params/",
          { report_type: reportType, staffcode: staffCode, usercode: userCode }
        );
        console.log("getWorkerReportParams result: ", res);
        console.log("after api called", reportType);
        if (reportType === "handset") {
          console.log("into handset function");
          let goldnk = res.data.data;
          this.dr_goldType = goldnk ? goldnk : [];
          console.log("Report Type handset: ", this.dr_goldType);
        } else if (reportType === "handover") {
          console.log("into handset function");
          let goldnk = res.data.data;
          this.dr_goldType = goldnk ? goldnk : [];
          console.log("Report Type handset: ", this.dr_goldType);
        } else if (reportType === "transaction") {
          let goldnk = res.data.data;
          this.de_goldType = goldnk ? goldnk : [];
          console.log("de_goldType: ", this.de_goldType);
          let workcut = res.data.data;
          this.de_workCut = workcut ? workcut : [];
          console.log("de_workCut: ", this.de_workCut);
          Loading.hide();
          return this.de_workCut;
        }  else if (reportType === "storage") {
          let goldnk = res.data.data;
          this.de_goldType = goldnk ? goldnk : [];
          console.log("de_goldType: ", this.de_goldType);
          // let workcut = res.data.data;
          // this.de_workCut = workcut ? workcut : [];
          // console.log("de_workCut: ", this.de_workCut);
          Loading.hide();
          return this.de_goldType;
        }
        Loading.hide();
        return this.dr_goldType;
      } catch (error) {
        console.log("getWorkerReportParams error", error.response);
        Loading.hide();
        return false;
      }
    },

    // For Worker: DeliverEnquiry(员工交收查询) & DeliverReport(员工交收报表二 (手工帐)) Page
    async getWorkerReportResult(
      reportType,
      staffCode,
      userCode,
      goldType,
      dateFrom,
      dateTo
    ) {
      Loading.show();
      try {
        console.log(
          "getWorkerReportResult: ",
          reportType,
          staffCode,
          userCode,
          goldType,
          dateFrom,
          dateTo
        );
        const res = await api.post(
          API_TSL_FRONTEND + "/report/WorkerReportView/" + "get_report_result/",
          {
            report_type: reportType,
            staffcode: staffCode,
            usercode: userCode,
            gold_type: goldType,
            date_from: dateFrom,
            date_to: dateTo,
          }
        );
        /*const res = await api.post(
          API_TSL_FRONTEND + "/report/WorkerReportView/" + "get_report_result/",
          {
            report_type: reportType,
            staffcode: staffCode,
            usercode: userCode,
            gold_type: "G750",
            date_from: "2023-03-22T16:01:02",
            date_to: "2023-04-21T15:22:55",
          }
        );*/
        Loading.hide();

        if (res.data.status){
          return res;
        }

      } catch (error) {
        console.log(error.response);
        Loading.hide();
        return false;
      }
    },

    // For QC and Dispatcher
    async getQCInventory(reportType, accID) {
      Loading.show();
      const params = new FormData();
      params.append("report_type", reportType);
      params.append("account_id", accID);
      try {
        console.log("params: ", reportType, accID);
        const res = await api.post(
          API_TSL_FRONTEND + "/report/QCInventoryReportView/" + "qcInventory/",
          params
        );
        console.log("getQCInventory: ", res);
        const data = res.data;
        if (reportType === "handset") {
          console.log('handset qc report', data.data);
          this.realtime_handset = data.data;
          console.log("this.realtime_handset: ", this.realtime_handset);
        } else if (reportType === "transaction") {
          const trans_res = data.data.checker_code;
          console.log("trans_res: ", trans_res);
          const workers = trans_res.worker;
          console.log("workers: ", workers);
          if (workers.length > 0) {
            for (let worker of workers) {
              let temp = {
                checkerRole: trans_res.role,
                checkerName: trans_res.name,
                dept: trans_res.department,
                workerName: worker.name,
                orderQty: worker.order_count,
                qty: worker.quantity,
              };
              console.log("temp: ", temp);
              this.realtime_transaction.push(temp);
            }
          } else {
            let tp = {
              checkerRole: trans_res.role,
              checkerName: trans_res.name,
              dept: trans_res.department,
              workerName: null,
              orderQty: null,
              qty: null,
            };
            this.realtime_transaction.push(tp);
          }

          console.log("this.realtime_transaction: ", this.realtime_transaction);
        }
        Loading.hide();
        return true;
      } catch (error) {
        console.log(error.response);
        Loading.hide();
        return false;
      }
    },

    // async getQCTransactionReport(searchReq, accId){
    //   console.log(accId);
    //    const res = await api.post(
    //       API_TSL_FRONTEND + "/report/QCReportView/" + "qcTransactionReport/",
    //       {
    //         account_pk: accId,
    //         deliverDate: searchReq.deliverDate,
    //         completeDate: searchReq.completeDate,
    //         site: searchReq.site,
    //         dept_id: searchReq.dept,
    //         orderNo: searchReq.orderNo,
    //         workerCode: searchReq.workerCode,
    //         checkerCode: searchReq.checkerCode
    //       }
    //     );
    //     if (res.data){
    //       return res.data
    //     }
    // }
  },
});
